package sisfo.astacala.model;

import java.util.List;
import org.hibernate.Session;
import sisfo.astacala.object.ObjectUser;
import sisfo.astacala.util.UtilHibernate;

public class ModelUser implements ModelInterface {

    private static ModelUser modelUser;

    public static ModelUser getInstance() {
        if (modelUser == null) {
            modelUser = new ModelUser();
        }
        return modelUser;
    }

    @Override
    public boolean insertModel(Object object) {
        boolean result;
        try {
            Session session = UtilHibernate.getSessionFactory().openSession();
            session.beginTransaction();
            session.save((ObjectUser) object);
            session.getTransaction().commit();
            session.close();
            result = true;
        } catch (Exception exception) {
//            exception.printStackTrace();
            result = false;
        }
        return result;
    }

    @Override
    public String updateModel(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String deleteModel(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object searchModel(Object object) {
        List userList;
        try {
            Session session = UtilHibernate.getSessionFactory().openSession();
            session.beginTransaction();
            userList = session.createQuery(object.toString()).list();
            session.getTransaction().commit();
            session.close();
        } catch (Exception exception) {
            userList = null;
            exception.printStackTrace();
        }
        return userList;
    }

    @Override
    public Object getAllModel(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
